#ifndef _UTILEX_SEM_HPP_
#define _UTILEX_SEM_HPP_

#include <crt_util.h>

#ifdef __cplusplus

class CSemHandle
{
public:
	CSemHandle() {
		_sem_init(&this->m_h, 0);
	}
	~CSemHandle()  {
		Close();
	}


	CSemHandle& operator=(CSemHandle& h) {

		if (this != &h)
		{
			Close();
			Attach(h.Detach());
		}
		return *this;
	}

	operator _sem_t() {
		return(m_h);
	}

	void Attach(_sem_t h) {
		m_h = h;
	}

	_sem_t Detach() {

		_sem_t h;
		h = m_h;
		return(h);
	}

	HRESULT Close(){

		_sem_destroy(&this->m_h);
		return S_OK;
	}

	HRESULT Wait() {

		_sem_wait(&this->m_h);
		return S_OK;
	}

	HRESULT WaitTime(int sec) {

		_sem_wait_time(&this->m_h,sec);
		return S_OK;
	}

	HRESULT Post() {

		_sem_post(&this->m_h);
		return S_OK;
	}

public:
	_sem_t m_h;
};



#endif


#endif
